package com.xygj.vueblog.shiro;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.xygj.vueblog.mapper.UserMapper;
import com.xygj.vueblog.entity.User;
import com.xygj.vueblog.service.UserService;
import com.xygj.vueblog.utils.JwtUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    JwtUtils jwtUtils;
    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean supports(AuthenticationToken token){

        return token instanceof JwtToken;
    }
    @Override
    //授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了 doGetAuthorizationInfo 进入角色授权");

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addStringPermission("user:add");
        //拿到当前登录的这个用户
        Subject subject = SecurityUtils.getSubject();
        User currentUser = (User) subject.getPrincipal();//拿到User对象
        //设置当前用户的权限
        info.addStringPermission(currentUser.getPerms());

        return info;
    }

    @Override
    //认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("执行了 doGetAuthenticationInfo 进入用户登录");

        JwtToken jwtToken = (JwtToken) token;

        String userId = jwtUtils.getClaimByToken((String) jwtToken.getPrincipal()).getSubject();

        User user = userService.getById(Integer.valueOf(userId));

        if (user == null){
            throw new UnknownAccountException("账户不存在");
        }

        if (user.getStatus() == -1){
            throw new LockedAccountException("账户已被锁定");
        }

        AccountProfile profile = new AccountProfile();
        BeanUtil.copyProperties(user,profile);

        return new SimpleAuthenticationInfo(profile,jwtToken.getCredentials(), getName());

        //用户名，密码 数据中取
//        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
//
//        User user = userMapper.findName(userToken.getUsername());
//        if (!userToken.getUsername().equals(user.getUsername())){
//            return null;//抛出异常 UnknownAccountException
//        }


//        Subject currentSubject = SecurityUtils.getSubject();
//        Session session = (Session) currentSubject.getSession();
//        session.setAttribute("loginUser", user);
        //密码认证，Shiro做
        //传入转换后的盐
//        return new SimpleAuthenticationInfo(user,user.getPassword(), user.getPassword());
    }
}
